package com.gang.mars.redisson.resource;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedissonResource {

    private final RedissonClient redissonClient;

    private final StringRedisTemplate stringRedisTemplate;

    public RedissonResource(RedissonClient redissonClient, StringRedisTemplate stringRedisTemplate) {
        this.redissonClient = redissonClient;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @GetMapping("/say")
    public String HL(){

        RLock lock = redissonClient.getLock("P2021120912");
        String stockStr;
        int stock = 0;
        lock.lock();
        try{
            stockStr = stringRedisTemplate.opsForValue().get("stock");
            stock = Integer.parseInt(stockStr);
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            lock.unlock();
        }

        final String s = "hello world " + stock;
        return s;
    }
}
